import uuid

import requests
from bs4 import BeautifulSoup
from Util import database
import prettytable

result = []
tableName = []
def getHtmlText(url):
    try:
        r = requests.get(url)
        r.raise_for_status()
        r.encoding = 'UTF-8'
        return r.text
    except:
        return 'error'
def getName(text):
    data = text.find_all('h2')
    for item in data:
        tableName.append(item.string.strip())
def getData(text):
    data = text.find_all('tr')
    for tr in data:
        allTd = tr.find_all('td')
        if len(allTd) == 0:
            continue
        single = []
        for td in allTd:
            newTd = td.string
            if (newTd == None):
                continue
            newTd = newTd.strip()
            single.append(newTd)
        if (len(single) <= 4):
            for i in range(4 - len(single)):
                single.append("Null")
        result.append(single)
def getUUid():
    uid = str(uuid.uuid4())
    suid = ''.join(uid.split('-'))
    return suid
def printList(num, start):
    x = prettytable.PrettyTable()
    x.field_names = ["排名", "中文名称", "中文简称", "主要品牌"]
    for i in range(num):
        x.add_row(result[i + start])
    x.set_style(prettytable.DEFAULT)
    print(x)

def main():
    url = "https://www.phb123.com/qiye/37060.html"
    html = getHtmlText(url)
    suop = BeautifulSoup(html, "html.parser")
    getData(suop)
    getName(suop)
    # print(tableName[0])
    # printList(100, 1)
    # print(tableName[1])
    # printList(20, 102)

    db = database("39.106.19.252", "lagou", "root", "lagou")

    table = "company"

    columns = ["id", "rank", "company_name", "company_shorter", "company_brand"]
    for i in range(100):
        value = []
        value.append(getUUid())
        value += result[i + 1]
        #print(value)
        db.insert(table, columns, value)
main()
